package com.qing.spark.dao

//import org.apache.hadoop.hbase.util.Bytes

import scala.collection.mutable.ListBuffer

/**
  * Created by Administrator on 2018/3/16 0016.
  */
case class HourCount(hour: Long, packageSize: Int, dataSize: Int)

object HourCountDao {
  val tableName = "hour_count"

  val family = "data"

  val qualifier1 = "packageSize"

  val qualifier2 = "dataSize"

  def save(lists: ListBuffer[HourCount]): Unit = {
    //    val table = HBaseUtils.getInstance.getTable(tableName)
    //    for (temp <- lists) {
    //      table.incrementColumnValue(Bytes.toBytes(temp.hour),
    //        Bytes.toBytes(family), Bytes.toBytes(qualifier1), temp.packageSize)
    //
    //      table.incrementColumnValue(Bytes.toBytes(temp.hour),
    //        Bytes.toBytes(family), Bytes.toBytes(qualifier2), temp.dataSize)
    //    }
    val con = MysqlUtils.getInstance.getCon()

    for (temp <- lists) {
      //          table.incrementColumnValue(Bytes.toBytes(temp.hour),
      //            Bytes.toBytes(family), Bytes.toBytes(qualifier1), temp.packageSize)
      //
      //          table.incrementColumnValue(Bytes.toBytes(temp.hour),
      //            Bytes.toBytes(family), Bytes.toBytes(qualifier2), temp.dataSize)

      val url = "select * from hour_count where hour=" + temp.hour
      MysqlUtils.getInstance.reCon()
      val result = con.prepareStatement(url).executeQuery()
      if (con != null && result.next()) {
        //已经存在
        val newDataSize = result.getLong("dataSize") + temp.dataSize
        val newPackageSize = result.getLong("packageSize") + temp.packageSize
        val psql = con.prepareStatement("update hour_count set dataSize = ?,packageSize = ? where hour = ?");
        psql.setLong(1, newDataSize)
        psql.setLong(2, newPackageSize)
        psql.setLong(3, temp.hour)
        psql.executeUpdate()
      } else {
        //不存在
        val sql = "insert into hour_count(hour,dataSize,packageSize) values(?,?,?)"
        val ptmt = con.prepareStatement(sql)
        ptmt.setLong(1, temp.hour)
        ptmt.setLong(2, temp.dataSize)
        ptmt.setLong(3, temp.packageSize)
        ptmt.execute()
      }
    }


  }
}